<?php

class Action
{

    public function cinema_zzb() {
        // 影院
        $path = ROOT_PATH . '/excel';

        $ZZB = _model('cinema_zzb')->getAll('SELECT * FROM `cinema_zzb`');

        $ZZB = api_helper::array_to_filelds_assoc('zzb_code', $ZZB);

        $DATA_CINEMA_ZZB = explode("\n", iconv('GBK', 'UTF-8//IGNORE', file_get_contents($path . '/cinema-zzb.csv')));

        array_shift($DATA_CINEMA_ZZB);

        $ASSISTANT = array();

        foreach ($DATA_CINEMA_ZZB as $k => $item) {
            $tmp = explode(',', $item);
            // 处理影院专员手机号
            $am = trim($tmp[11]);
            if (preg_match('/[-]/', $am)) {
                $am = str_replace('-', '', $am);
                $tmp[11] = $am;
            }

            // 影院专员手机号手机
            if ($am && !in_array($am, $ASSISTANT)) {
                $ASSISTANT[] = $am;
            }

            $DATA_CINEMA_ZZB[$k] = $tmp;
        }

        // TODO 获取影院专员id
        $ASSISTANT_IDS = _model('user')->getList(array('user_name' => $ASSISTANT));
        if (count($ASSISTANT_IDS) !== count($ASSISTANT)) {
            // exit(nl2br('匹配不到影院专员' . "\n" . join("\n", array_diff($ASSISTANT, $ASSISTANT_IDS))));
        }

        $ASSISTANT_IDS = api_helper::array_to_filelds_assoc('user_name', $ASSISTANT_IDS);

        $DATA = array();
        $NOT_IN_ZZB_TABLE = array();
        $CONTRACT = array();

        foreach ($DATA_CINEMA_ZZB as $key => $item_detail) {
            $tmp = array(
                'store_id' => trim($item_detail[0]),
                'user_id' => trim($item_detail[1]),
                'title' => trim($item_detail[2]),
                'manager' => trim($item_detail[3]),
                'manager_mobile' => trim($item_detail[4]),
                'address' => trim($item_detail[5]),
                'agency_id' => trim($item_detail[6]),
                'agency_name' => trim($item_detail[7]),
                'agency_mobile' => trim($item_detail[8]),
                'zzb_code' => trim($item_detail[9]),
                'assistant_name' => trim($item_detail[10]),
                'assistant_mobile' => trim($item_detail[11]),
                'is_allow_period' => trim($item_detail[12]),
                'contract_number' => trim($item_detail[13]),
            );

            if (isset($ASSISTANT_IDS[$tmp['assistant_mobile']])) {
                $tmp['assistant_id'] = $ASSISTANT_IDS[$tmp['assistant_mobile']]['id'];
            }

            // 处理账期
            switch ($tmp['is_allow_period']) {
                case '是':
                    $tmp['is_allow_period'] = 1;
                    break;
                case '否':
                default:
                    $tmp['is_allow_period'] = 0;
                    break;
            }

            // 处理合同号
            if (!preg_match('/^[A-Z]+\d{3}$/', $tmp['contract_number'])) {

                $tmp['contract_number'] = strtoupper(str_replace(array(' ', '-'), '', $tmp['contract_number']));

                if (preg_match('/\//', $tmp['contract_number'])) {
                    // 一家影院多合同
                    $tmp_C = explode('/', $tmp['contract_number']);
                    foreach ($tmp_C as $c) {
                        $CONTRACT[] = array(
                            'store_id' => $tmp['store_id'],
                            'contract_id' => $c,
                        );
                    }

                    unset($tmp['contract_number']);

                } else {

                    if (preg_match('/^([A-Z]+)(\d{1,2})$/', $tmp['contract_number'], $match)) {
                        $tmp['contract_number'] = $match[1] . '0' . $match[2];
                    }

                    switch ($tmp['contract_number']) {
                        case '无合同已合作':
                        case '已合作无合同':
                        case '未见合同':
                        case '合同丢失':
                        case '未签约':
                            unset($tmp['contract_number']);
                            break;
                        default:
                            //echo '行' . ($key + 2) . "\t" . '名称' . $tmp['title'] . "\t" . $tmp['contract_number'] . "\n";
                            break;
                    }
                }
            }

            // 正常的合同
            if (!empty($tmp['contract_number'])) {
                $CONTRACT[] = array(
                    'store_id' => $tmp['store_id'],
                    'contract_id' => $tmp['contract_number']
                );
            }

            // 填入专资名称
            if (is_numeric($tmp['zzb_code']) && array_key_exists($tmp['zzb_code'], $ZZB)) {
                $tmp['zzb_name'] = $ZZB[$tmp['zzb_code']]['cinema_name'];
                $DATA[] = $tmp;
            } else {
                $NOT_IN_ZZB_TABLE[] = $tmp;
            }
        }

        // var_dump($DATA);

        // TODO 写入合同表

        // TODO 写入user表

        $user_rule = array(
            'assistant_id' => ''
        );
        foreach ($DATA as $item) {
            $data = parse_param($user_rule, $item);
            var_dump($item['user_id'], $data);
        }

        // TODO 写入store表
        $store_rule = array(
            'zzb_code' => ''
        );
        foreach ($DATA as $item) {
            $data = parse_param($store_rule, $item);
            //var_dump($item['store_id'], $data);
        }
    }

    public function import() {
        $file = fopen("/data/import/u8.csv","r") or exit("Unable to open file!");
        /*
         * APP ID, U8客户编码, 开票抬头（U8客户简称）, 专资编码, 合同编号
         */
        $row = 1;
        while (($data = fgetcsv($file, 1000, ",")) !== FALSE) {
            //写入数据库
            $store = _model('store')->update($data[0], array('zzb_id' => $data[3]));

            $u8 = _model('store_u8')->read(array('store_id' => $data[0]));
            if($u8){
                _model('store_u8')->update(array('store_id' => $data[0]), array('u8_id' => $data[1], 'u8_name' => $data[2]));
            } else {
                _model('store_u8')->create(array('store_id' => $data[0], 'u8_id' => $data[1], 'u8_name' => $data[2]));
            }

            $contract = _model('store_contract')->read(array('store_id' => $data[0]));
            if($contract){
                _model('store_contract')->update(array('store_id' => $data[0]), array('code' => $data[4]));
            } else {
                _model('store_contract')->create(array('store_id' => $data[0], 'code' => $data[4], 'add_time' => date('Y-m-d H:i:s')));
            }

            if($store){
                echo "<p>$row:success</p>\n";
            }else{
                echo "<p>$row:fail</p>\n";
            }
            $row++;
        }
        fclose($file);
    }
}
?>